package com.shixulin.controller;

import com.shixulin.domain.ResponseResult;
import com.shixulin.entity.DTO.NewsQueryDTO;
import com.shixulin.entity.News;
import com.shixulin.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@RestController
@RequestMapping("/teacher")
public class NewsController {
    @Autowired
    private NewsService newsService;
    @PostMapping("/uploadNews")
    public ResponseResult uploadNews(@RequestBody News news){
        String inputDateTimeString =news.getTime();
        // 解析时间字符串
        Instant instant = Instant.parse(inputDateTimeString);
        LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.of("UTC"));
        // 定义新的日期时间格式
        DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd ");
        // 格式化为新的日期时间字符串
        String formattedDateTime = dateTime.format(newFormatter);
        News news_data = new News(null,news.getNewsTitle(),news.getCoverUrl(),news.getNewsUrl(),formattedDateTime);
        return newsService.uploadNews(news_data);
    }

    @GetMapping("/getNews")
    public ResponseResult uploadNews(){
        return newsService.getNews1();
    }
    //分页查询
     @PostMapping("/getNews")
     public ResponseResult getNews(@RequestBody NewsQueryDTO newsQueryDTO){
        return newsService.getNews(newsQueryDTO);
     }

    @GetMapping("/selectOneNews/{id}")
    public ResponseResult selectOneNews(@PathVariable("id") int id){
        return newsService.selectOneNews(id);
    }
    @PostMapping("/updateNews")
    public ResponseResult updateNews(@RequestBody News news){
        return newsService.updateNews(news);
    }
    @GetMapping("/deleteNews/{id}")
    public ResponseResult deleteNews(@PathVariable("id") int id){
        return newsService.deleteNews(id);
    }


}
